package com.simple.mqtt.server.config;

import com.simple.mqtt.server.auth.IMqttAuthHandler;
import com.simple.mqtt.server.channel.ChannelManager;
import com.simple.mqtt.server.dispatcher.IMqttMessageDispatcher;
import com.simple.mqtt.server.dispatcher.DefaultMqttMessageDispatcher;
import com.simple.mqtt.server.serializer.IMessageSerializer;
import com.simple.mqtt.server.session.IMqttSessionManager;
import com.simple.mqtt.server.store.IMqttMessageStore;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.ExecutorService;

/**
 * MQTT代理配置类，包含所有服务器配置参数
 */
@Getter
public class MqttBrokerConfig {
    // 监听主机地址
    private final String host;
    // 监听端口
    private final int port;
    // Boss线程数
    private final int bossThreads;
    // Worker线程数
    private final int workerThreads;
    // 业务线程数
    private final int businessThreads;
    // 心跳超时时间（毫秒）
    private final long keepAliveTimeout;
    // 最大消息大小（字节）
    private final int maxBytesInMessage;
    // 最大客户端ID长度
    private final int maxClientIdLength;
    // 会话管理器
    private final IMqttSessionManager sessionManager;
    // 消息存储
    private final IMqttMessageStore messageStore;
    // 认证处理器
    private final IMqttAuthHandler authHandler;
    // 消息序列化器
    private final IMessageSerializer serializer;
    // 所有客户端连接的通道管理器
    private final ChannelManager channelManager;
    // 消息分发的消息调度器
    private final IMqttMessageDispatcher messageDispatcher;
    
    // 代理实例引用，用于回调
    @Setter
    private transient com.simple.mqtt.server.core.MqttBroker broker;

    /**
     * 构造函数
     * @param host 监听主机地址
     * @param port 监听端口
     * @param bossThreads boss线程数
     * @param workerThreads worker线程数
     * @param businessThreads 业务线程数
     * @param keepAliveTimeout 心跳超时时间（毫秒）
     * @param maxBytesInMessage 最大消息大小（字节）
     * @param maxClientIdLength 最大客户端ID长度
     * @param sessionManager 会话管理器
     * @param messageStore 消息存储
     * @param authHandler 认证处理器
     * @param serializer 消息序列化器
     * @param businessExecutor 业务线程池
     */
    public MqttBrokerConfig(String host, int port, int bossThreads, int workerThreads, int businessThreads,
                     long keepAliveTimeout, int maxBytesInMessage, int maxClientIdLength,
                     IMqttSessionManager sessionManager, IMqttMessageStore messageStore,
                     IMqttAuthHandler authHandler, IMessageSerializer serializer, ExecutorService businessExecutor) {
        // 设置基本配置参数
        this.host = host;
        this.port = port;
        this.bossThreads = bossThreads;
        this.workerThreads = workerThreads;
        this.businessThreads = businessThreads;
        this.keepAliveTimeout = keepAliveTimeout;
        this.maxBytesInMessage = maxBytesInMessage;
        this.maxClientIdLength = maxClientIdLength;
        
        // 设置核心组件
        this.sessionManager = sessionManager;
        this.messageStore = messageStore;
        this.authHandler = authHandler;
        this.serializer = serializer;
        
        // 初始化辅助组件
        this.channelManager = new ChannelManager();
        this.messageDispatcher = new DefaultMqttMessageDispatcher(this, businessExecutor);
    }
}

